﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NovoSerDes.Funcs
{
    internal class IniHelp
    {
        private string _filePath;

        public IniHelp(string filePath)
        {
            _filePath = filePath;
        }
        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filepath);
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        /// <summary>
        /// 读配置文件
        /// </summary>
        /// <param name="section">段名</param>
        /// <param name="key">键名</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <param name="filepath">配置文件INI地址</param>
        /// <returns></returns>
        public  String ReadIni(string section, string key, string def, int size)
        {
            StringBuilder sb = new StringBuilder();
            long l = GetPrivateProfileString(section, key, def, sb, size, _filePath);
            return sb.ToString();
        }
        /// <summary>
        /// 写入数据到配置文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public  long WriteIni(string section, string key, string val)
        {
            return WritePrivateProfileString(section, key, val, _filePath);
        }
        // 删除ini文件下所有段落
        public void ClearAllSection()
        {
            WriteIni(null, null, null);
        }
        //删除ini文件下personal段落下的所有键
        public void ClearSection(string Section)
        {
            WriteIni(Section, null, null);
        }


        /// <summary>
        /// 获取某个指定节点(Section)中所有KEY和Value
        /// </summary>
        /// <param name="lpAppName">节点名称</param>
        /// <param name="lpReturnedString">返回值的内存地址,每个之间用\0分隔</param>
        /// <param name="nSize">内存大小(characters)</param>
        /// <param name="lpFileName">Ini文件</param>
        /// <returns>内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够</returns>
        [DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]

        private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
        /// <summary>
        /// 获取INI文件指定Section节点下的数据，并返回 Dictionary 对象
        /// </summary>
        /// <param name="iniFile">ini文件路径</param>
        /// <param name="section">section节点</param>
        /// <returns></returns>
        public Dictionary<string, string> GetSectionDic(string iniFile, string section)
        {
            uint MAX_BUFFER = 32767;
            string[] items = null;
            //分配内存
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
            uint bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);
            if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
            {
                string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
                items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            }
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pReturnedString);     //释放内存
                                                                                       //无结果返回空
            if (items == null)
            {
                return null;
            }
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (string item in items)
            {
                if (!item.Contains("="))
                {
                    continue;
                }
                string[] part = item.Split('=');
                dic.Add(part[0], part[1]);
            }
            return dic;
        }

        public List<string> GetAllSections()
        {
            List<string> sections = new List<string>();
            const int bufferSize = 65536; // 缓冲区大小
            StringBuilder buffer = new StringBuilder(bufferSize);

            // 调用 GetPrivateProfileString 获取所有段名
            long result = GetPrivateProfileString(null, null, "", buffer, bufferSize, _filePath);
            if (result > 0)
            {
                string[] sectionNames = buffer.ToString().Split('\0');
                foreach (string section in sectionNames)
                {
                    if (!string.IsNullOrWhiteSpace(section))
                    {
                        sections.Add(section);
                    }
                }
            }

            return sections;
        }
    }



}
